package com.jie.eduservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.commonutils.ResultEntity;
import com.jie.eduservice.entity.EduTeacher;
import com.jie.eduservice.entity.vo.TeacherQueryVO;
import com.jie.eduservice.service.EduTeacherService;
import com.jie.servicebase.exception.GuLiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xzj
 * @since 2020-10-01
 */
//@CrossOrigin  // 解决跨域问题
@RequestMapping("/eduservice/teacher")
@RestController
public class EduTeacherController {

    private final EduTeacherService teacherService;

    @Autowired
    public EduTeacherController(EduTeacherService teacherService) {
        this.teacherService = teacherService;
    }

    // 8.测试异常的方法
    @GetMapping("/exception")
    public ResultEntity exception() {
        throw new GuLiException(20001, "执行了自定义异常处理...");
    }

    // 7.修改讲师信息
    @PostMapping("/updateTeacher")
    public ResultEntity updateTeacher(
            // 需要有id的讲师对象以此来修改讲师(由前端来设置id)
            @RequestBody EduTeacher eduTeacher
    ) {
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag) {
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

    // 6.根据id查询讲师
    @GetMapping("/getTeacher/{id}")
    public ResultEntity getTeacher(
            @PathVariable("id") String id
    ) {
        EduTeacher eduTeacher = teacherService.getById(id);
        // 以下这段注释的代码是做异常处理的测试代码
//        if (eduTeacher != null) {
//            throw new RuntimeException("抛出异常");
//        }
//        if (eduTeacher != null) {
//            throw new ArithmeticException("算数处理异常...");
//        }
        return ResultEntity.ok().data("teacher", eduTeacher);
    }

    // 5.添加讲师
    @PostMapping("/addTeacher")
    public ResultEntity addTeacher(
            // 传入的要新增的讲师信息
            @RequestBody EduTeacher eduTeacher
    ) {
        boolean flag = teacherService.save(eduTeacher);
        if (flag) {
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

    // 4.带条件的分页查询
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public ResultEntity pageTeacherCondition(
            // 当前页
            @PathVariable("current") Long current,
            // 每页记录数
            @PathVariable("limit") Long limit,
            // 封装的条件(如果要加@RequestBody, 那么要是Post请求)
            @RequestBody(required = false) TeacherQueryVO teacherQueryVO
    ) {
        Page<EduTeacher> teacherPage = teacherService.pageTeacherCondition(current, limit, teacherQueryVO);
        Long total = teacherPage.getTotal();  // 获得总记录数
        List<EduTeacher> records = teacherPage.getRecords();  // 获得数据list集合
        return ResultEntity.ok().data("total", total).data("rows", records);
    }

    // 3.分页查询
    @GetMapping("/pageTeacher/{current}/{limit}")
    public ResultEntity pageListTeacher(
            // 当前页
            @PathVariable("current") Long current,
            // 每页记录数
            @PathVariable("limit") Long limit
    ) {
        // 创建Page对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        // 调用方法实现分页
        teacherService.page(teacherPage, null);
        Long total = teacherPage.getTotal();  // 获得总记录数
        List<EduTeacher> records = teacherPage.getRecords();  // 获得数据list集合
        return ResultEntity.ok().data("total", total).data("rows", records);
    }

    // 2.删除讲师(逻辑删除)
    @DeleteMapping("/{id}")
    public ResultEntity removeTeacher(@PathVariable("id") String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

    // 1.查询讲师表中所有数据
    @GetMapping("/findAll")  // RequestMapping加了个Get
    public ResultEntity findAllTeacher() {
        // 调用service的方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list();
        return ResultEntity.ok().data("items", list);
    }

}

